//
//  JJProgressRateRoundView.m
//  进度条绘制
//
//  Created by jxf on 15/12/8.
//  Copyright © 2015年 JJ.sevn. All rights reserved.
//

#import "JJProgressRateRoundView.h"

@interface JJProgressRateRoundView ()
@property(nonatomic, weak) UILabel * label;
@end
@implementation JJProgressRateRoundView

+ (instancetype)progressRateRoundViewWithType:(JJProgressRateRoundViewType)type
{
    JJProgressRateRoundView *view = [[JJProgressRateRoundView alloc] init];
    view.progressRateRoundViewtype = type;
    return view;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self initView];
    }
    return self;
}

- (void)awakeFromNib
{
    [self initView];
}

- (void)initView
{
    UILabel *label = [[UILabel alloc] init];
    [self addSubview:label];
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:12];
    self.label = label;
    
    self.backgroundColor = [UIColor grayColor];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.label.frame = self.bounds;
}

- (void)setProgressValue:(CGFloat)progressValue
{
    _progressValue = progressValue;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    CGPoint center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
    CGFloat radius = self.frame.size.width * 0.5 - 10;
    CGFloat endAngle = M_PI * 2 * self.progressValue;
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle:endAngle clockwise:YES];
    if (self.progressRateRoundViewtype == kJJProgressRateRoundViewFill) {
        [path addLineToPoint:center];
        [path fill];
    }else{
        [path stroke];
    }
    self.label.text = [NSString stringWithFormat:@"%.0f%%", self.progressValue * 100];
}

- (void)setRateLabelFont:(UIFont *)rateLabelFont
{
    _rateLabelFont = rateLabelFont;
    self.label.font = rateLabelFont;
}

- (void)setRateLabelTextColor:(UIColor *)rateLabelTextColor
{
    _rateLabelTextColor = rateLabelTextColor;
    self.label.textColor = rateLabelTextColor;
}
@end
